package com.yiqi.lottery.feature.mine.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.CharsBo;
import com.yiqi.lottery.entity.bo.CouprceBo;
import com.yiqi.lottery.feature.lottery.view.adapter.BuyHallAdapter;
import com.yiqi.lottery.feature.mine.contract.NumberContract;
import com.yiqi.lottery.feature.mine.dependencies.number.DaggerNumberComponent;
import com.yiqi.lottery.feature.mine.dependencies.number.NumberPresenterModule;
import com.yiqi.lottery.feature.mine.view.adapter.NUmberAdapter;

import javax.inject.Inject;

public class NumberManageActivity extends BaseActivity implements NumberContract.View, BaseQuickAdapter.OnItemClickListener, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_number)
    RecyclerView mRvNumber;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @Inject
    NumberContract.Presenter mPresenter;

    private EmptyView mEmptyView;
    PageData<CharsBo> mData;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int mPageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private NUmberAdapter mAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_number_manage;
    }

    @Override
    protected void initInjector() {
        DaggerNumberComponent.builder().numberPresenterModule(new NumberPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "追号管理", true, true);
        initAdapter();
    }

    @Override
    protected void initData() {
        mPresenter.getMyCharsList(mPageIndex);
    }

    @Override
    public void showMyBanlanceSuccess(PageData<CharsBo> msg) {
        mRefreshLayout.finishRefresh();
        mData = msg;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMyBanlanceError(String msg) {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(msg));
        mAdapter.setNewData(null);
    }

    @Override
    public void showMyBanlanceMoreSuccess(PageData<CharsBo> msg) {
        isErr = false;
        mAdapter.addData(msg.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showMyBanlanceMoreError(String msg) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showMyBanlanceEmpty() {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无追号数据！"));
        mAdapter.setNewData(null);
    }

    @Override
    public void showNumberDetailSuccess(CharsBo msg) {

    }

    private void initAdapter() {
        mAdapter = new NUmberAdapter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        mAdapter.isFirstOnly(false);
        mEmptyView = new EmptyView(mRvNumber);
        mRvNumber.setLayoutManager(new LinearLayoutManager(this));
        mRvNumber.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter.bindToRecyclerView(mRvNumber);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mAdapter.setOnLoadMoreListener(this);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent
                = new Intent(this, NUmberDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getOrder_id());
        startActivity(intent);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPageIndex = 0;
        initData();
    }

    @Override
    public void onLoadMoreRequested() {
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    mPageIndex++;

                    mPresenter.getMyCharsMoreList(mPageIndex);

                }
            }
        }
    }
}
